package com.pcbly.web.components.utils.jbpm;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.identity.Group;
import org.jbpm.api.identity.User;
import org.jbpm.pvm.internal.identity.spi.IdentitySession;

import com.pcbly.web.bean.system.GroupBean;
import com.pcbly.web.bean.user.UserBean;
import com.pcbly.web.business.group.GroupService;
import com.pcbly.web.business.user.UserService;
import com.pcbly.web.components.utils.LightsoftUtils;

@SuppressWarnings("unchecked")
public class UserSession implements IdentitySession {

	private UserService userService;
	private GroupService groupService;
	private static Log logger = LogFactory.getLog(UserSession.class);

	public UserSession() {
		userService = (UserService) LightsoftUtils.getBean("appUserService");
		groupService = (GroupService) LightsoftUtils.getBean("appRoleService");
	}

	public String createGroup(String s, String s1, String s2) {
		return null;
	}

	public void createMembership(String s, String s1, String s2) {
	}

	public String createUser(String s, String s1, String s2, String s3) {
		return null;
	}

	public void deleteGroup(String s) {
	}

	public void deleteMembership(String s, String s1, String s2) {
	}

	public void deleteUser(String s) {
	}

	public Group findGroupById(String id) {
		try {
			GroupBean groupBean = groupService.getGroupBeanById(id);
			return (Group) groupBean;
		} catch (Exception e) {
			// e.printStackTrace();
			logger.error(e.getMessage());
		}
		return null;
	}

	public List findGroupsByUser(String id) {
		try {
			List<GroupBean> groupBeans = userService.findGroupBeanByUserId(id);
			List<Group> list = new ArrayList<Group>();
			for (GroupBean groupBean : groupBeans) {
				list.add((Group) groupBean);
			}
			return list;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}

	public List findGroupsByUserAndGroupType(String s, String s1) {
		return findGroupsByUser(s);
	}

	public User findUserById(String id) {
		try {
			UserBean user = userService.getUserBeanById(id);
			return (User) user;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}

	public List findUsers() {
		try {
			List<UserBean> userBeans = userService.findUserBeanAll();
			return userBeans;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}

	public List findUsersByGroup(String id) {
		try {
			List<UserBean> userBeans = userService.findUserBeanByGroupId(id);
			return userBeans;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}

	}

	public List findUsersById(String... as) {
		return null;
	}

}
